<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/22
 * Time: 下午7:54
 */
namespace Home\Controller;
use Common\Controller\HomeBaseController;

class CollectionController extends HomeBaseController
{
    public function index()
    {
        $this->assign('title','FD商城--我的收藏');
        $this->display();
    }

    public function collect()
    {
        if(IS_GET){
            if(empty(session(C('FRONT_SESSION')))){
                $this->ajaxReturn(['message'=>'请先去登陆！']);
            }
            $goods_id = I('get.id',0);
            $user_id = session(C('FRONT_SESSION'))['id'];
            if(empty($goods_id)){
                $this->ajaxReturn(['message'=>'非法操作！']);
            }
            $collect = M('collection');
            if($collect->where(['goods_id'=>$goods_id,'mid'=>$user_id])->getField('id')){
                $this->ajaxReturn(['message'=>'已经收藏，无需再次收藏！']);
            }
            //获取商品信息
            $goods_info = D('goods')->get_one_info(['id'=>$goods_id],'goods_name,fd_price');
            if(!isset($goods_info['fd_price'])){
                $this->ajaxReturn(['message'=>'获取该商品信息失败！']);
            }
            $data = [
                'goods_id'      =>  $goods_id,
                'mid'           =>  $user_id,
                'goods_name'    =>  $goods_info['goods_name'],
                'price'         =>  $goods_info['fd_price'],
                'add_time'      =>  time()
            ];
            if($collect->add($data)){
                $this->ajaxReturn(['message'=>'收藏成功！']);
            }
            $this->ajaxReturn(['message'=>'收藏失败！']);
        }else{
            $this->ajaxReturn(['message'=>'非法操作！']);
        }
    }
}